package org.toy.maxent;

import java.util.ArrayList;
import java.util.List;

import opennlp.maxent.ContextGenerator;

/**
 * 
 * @author weip.pengw
 * @time 2010-9-1 下午10:05:33
 */
public class MultiFieldContextGenerator implements ContextGenerator {

	/**
	 * 
	 * @see opennlp.maxent.ContextGenerator#getContext(java.lang.Object)
	 */
	public String[] getContext(Object o) {
		List<String> list = (List<String>) o;
		List<String> featureList = new ArrayList<String>();
		for (String s : list) {
			int index = s.indexOf(':');
			String key = s.substring(0, index);
			if (index == s.length() - 1) {
				continue;
			}
			String value = s.substring(index + 1);
			String[] featureValue = this.getSplitwords(value);
			for (String f : featureValue) {
				if("".equals(f)){
					continue;
				}
				featureList.add(new StringBuffer().append(key).append(":")
						.append(f).toString());
			}
		}
		String[] featureArray = new String[featureList.size()];
		return featureList.toArray(featureArray);
	}
	
	protected String[] getSplitwords(String value){
		return (String[])value.split(" ");
	}
	
	

}
